package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.system.domain.SysUnit;
import com.ruoyi.system.domain.SysCourse;
import com.ruoyi.system.domain.SysTextbook;
import com.ruoyi.system.mapper.SysCourseMapper;
import com.ruoyi.system.mapper.SysTextbookMapper;
import com.ruoyi.system.mapper.SysUnitMapper;
import com.ruoyi.system.service.ISysUnitService;

/**
 * 课程单元Service业务层处理
 * 
 * @author ruoyi
 */
@Service
public class SysUnitServiceImpl implements ISysUnitService 
{
    @Autowired
    private SysUnitMapper unitMapper;
    @Autowired
    private SysTextbookMapper sysTextbookMapper;
    @Autowired
    private SysCourseMapper sysCourseMapper;

    /**
     * 查询单元列表
     * 
     * @param unit 单元信息
     * @return 单元集合
     */
    @Override
    public List<SysUnit> selectList(SysUnit unit)
    {
        return unitMapper.list(unit);
    }

    /**
     * 查询单元信息
     * 
     * @param unitId 单元ID
     * @return 单元信息
     */
    @Override
    public SysUnit selectUnitById(Long unitId)
    {
        return unitMapper.selectById(unitId);
    }

    /**
     * 新增单元
     * 
     * @param unit 单元信息
     * @return 结果
     */
    @Override
    public int insert(SysUnit unit)
    {
        unit.setWordCount(0);
        return unitMapper.insert(unit);
    }

    /**
     * 修改单元
     * 
     * @param unit 单元信息
     * @return 结果
     */
    @Override
    public int update(SysUnit unit)
    {
        return unitMapper.update(unit);
    }

    /**
     * 批量删除单元
     * 
     * @param unitIds 需要删除的单元ID
     * @return 结果
     */
    @Override
    public int deleteByIds(Long[] unitIds)
    {
        return unitMapper.deleteByIds(unitIds);
    }

    /**
     */
    @Override
    public List<TreeSelect> courseTreeList(SysCourse sysCourse)
    {
        
        // 添加顶级【全部教材】
        List<TreeSelect> listRoot = new ArrayList<TreeSelect>();
        {
        	Long id = 0L;
        	String label = "全部教材";
        	boolean disabled = false;
        	Integer lev = 0;
        	List<TreeSelect> children = getTextbookTree();
            listRoot.add(new TreeSelect(id, label, disabled, lev, children));
        }
        return listRoot;
    }
    
    public List<TreeSelect> getTextbookTree()
    {
        List<SysTextbook> listSysTextbook = sysTextbookMapper.selectList(new SysTextbook());
        List<SysCourse> listSysCourse = sysCourseMapper.list(new SysCourse());
        
        // 添加教材节点列表
        List<TreeSelect> listTextbookTree = new ArrayList<TreeSelect>();
        for(SysTextbook sysTextbook : listSysTextbook) {
        	Long id = sysTextbook.getTextbookId();
        	String label = sysTextbook.getTextbookName();
        	boolean disabled = false;
        	Integer lev = 1;
        	List<TreeSelect> children = getCourseTree(sysTextbook, listSysCourse);
        	listTextbookTree.add(new TreeSelect(id, label, disabled, lev, children));
        }
        return listTextbookTree;
    }
    
    public List<TreeSelect> getCourseTree(SysTextbook sysTextBook, List<SysCourse> listSysCourse)
    {
        
        // 添加教材节点列表
        List<TreeSelect> listCourseTree = new ArrayList<TreeSelect>();
        for(SysCourse sysCourse : listSysCourse) {
        	if (sysCourse.getTextbookId().longValue() != sysTextBook.getTextbookId().longValue()) {
				continue;
			} else {
	        	Long id = sysCourse.getCourseId();
	        	String label = sysCourse.getCourseName();
	        	boolean disabled = false;
	        	Integer lev = 2;
	        	List<TreeSelect> children = null;
	        	listCourseTree.add(new TreeSelect(id, label, disabled, lev, children));
			}
        }
        return listCourseTree;
    }
}